<template>
  <div style="padding:30px;">
    <Breadcrumb />
    <el-card style="margin-top:15px;">
      <el-button type="primary" @click="addDialogVisible = true">添加分类</el-button>
      <el-table
        :data="tableData"
        style="width: 100%;margin-top:15px;"
        border
        row-key="cat_id"
        lazy
        :tree-props="{children: 'children'}"
      >
        <el-table-column
          label="#"
          type="index"
        >
          <template slot-scope="{row}">
            {{ row.id }}
          </template>
        </el-table-column>
        <el-table-column
          label="分类名称"
        >
          <template slot-scope="{ row }">
            <span>{{ row.cat_name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="是否有效"
        >
          <template slot-scope="{ row }">
            <i :class="!row.cat_deleted?'el-icon-success':'el-icon-error'" />
          </template>
        </el-table-column>
        <el-table-column
          label="排序"
        >
          <template slot-scope="{ row }">
            <el-tag :type="tagType(row)">
              {{ levelArr[row.cat_level] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template slot-scope="{row}">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="onEdit(row)">修改</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="delClassify(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="query.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 添加分类弹层 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form ref="add" :model="params" label-width="100px" class="demo-dynamic">
        <el-form-item
          prop="cat_name"
          label="分类名称"
          :rules="[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
          ]"
        >
          <el-input v-model="params.cat_name" />
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            ref="check"
            v-model="classify"
            style="width:100%;"
            :options="goodsClassify"
            :props="defaults"
            @focus="getGoodsClassify"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoodsClassify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { goodsClassify, deleteClassifyById, addClassify } from '@/api/categories.js'

export default {
  name: 'GoodsClassify',
  data() {
    return {
      query: {
        pagenum: 1,
        pagesize: 5
      },
      addDialogVisible: false,
      tableData: [],
      levelArr: {
        0: '一级',
        1: '二级',
        2: '三级'
      },
      addInput: '',
      total: 0,
      classify: [],
      goodsClassify: [],
      defaults: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      },
      params: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      }
    }
  },
  computed: {
    tagType() {
      return function(row) {
        if (row.cat_level === 0) {
          return ''
        }
        if (row.cat_level === 1) {
          return 'success'
        }
        return 'warning'
      }
    }
  },
  created() {
    this.loadList()
  },

  methods: {
    // 获取商品分类列表
    async loadList() {
      const res = await goodsClassify(this.query)
      console.log(res)
      this.tableData = res.result
      this.total = res.total
      this.tableData.forEach((item, index) => { item.id = index + 1 })
    },
    // 删除分类
    async delClassify(row) {
      try {
        await deleteClassifyById(row.cat_id)
        this.$message.success('删除成功')
        this.loadList()
      } catch (err) {
        console.log(err)
      }
    },
    handleSizeChange(val) {
      this.query.pagesize = val
      this.loadList()
    },
    handleCurrentChange(val) {
      this.query.pagenum = val
      this.loadList()
    },
    // 拿到商品分类列表
    async getGoodsClassify() {
      const res = await goodsClassify({ type: 2 })
      console.log(res)
      this.goodsClassify = res
    },
    // 点击确认添加分类
    async addGoodsClassify() {
      if (!this.classify.length) {
        await addClassify(this.params)
      } else if (this.classify.length === 1) {
        this.params.cat_pid = this.classify.join('')
        this.params.cat_level = 1
        await addClassify(this.params)
      } else {
        this.params.cat_pid = this.classify[this.classify.length - 1]
        this.params.cat_level = 2
        await addClassify(this.params)
      }
      this.addDialogVisible = false
      this.loadList()
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .has-gutter {
  background-color: #ccc;
  color: #333;
}
::v-deep .el-table__expand-icon {
  transform: rotate(0deg);
  .el-icon-arrow-right:before {
    content:'\e6d9';
    border: 1px solid #ccc;
  }
}
::v-deep .el-table__expand-icon--expanded{
  .el-icon-arrow-right:before {
    content:'\e6d8';
  }
}
.el-icon-success {
  color: green;
}
.el-icon-error {
  color: red;
}
</style>

